
export const init = (self, tiyData) => {
  tiyData.tPage.currentPage = 1;
  tiyData.tPage.stepNumber = 40;
  tiyData.tParam.postageName = '';
}

export const webList = (self, tiyData) => {
  self.$Spin.show();
  tiyData.tData.postageList.splice(0,tiyData.tData.postageList.length );
  self.$apiOther.getPostageList({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "postageName": tiyData.tParam.postageName,
    "method":tiyData.tParam.method,
  }).then(function (response) {
      self.$Spin.hide();
      if (response.success) {
        tiyData.tParam.length = [];
        tiyData.tData.postageList.length = 0;
        let postagePage = response.result;
        tiyData.tPage.total = postagePage.total;
        tiyData.tPage.stepNumber = postagePage.pageSize;
        postagePage.list.forEach(
          function (item) {
            tiyData.tData.postageList.push(item);
          }
        );
        if (0 == tiyData.tData.postageList.length) {
          self.$Message.error("查询没有结果");
        }
      } else {
        self.$Spin.hide();
        self.$Message.error(response.errorMessage);
      }
    });
};

export const savePostage=(self,tiyData)=> {
  tiyData.tStatus.modifyModal.isLoading = true;
  //验证运费配置规则
  let preConfig;//上一档配置信息

  let errorInput = "";
  let ruleSize=tiyData.tParam.postage.postageRules.length;
  let flag = true;
  for(let i=0;i<tiyData.tParam.postage.postageRules.length;i++){
    let item = tiyData.tParam.postage.postageRules[i];

    if(!item.firstWeight>0) {
      self.$refs['firstWeight'+i][0].focus();
      flag = false;
      break ;
    }else if(item.tieredWeight < 0){
      self.$refs['tieredWeight'+i][0].focus();
      flag = false;
      break ;
    }else if(item.tieredFee < 0){
      self.$refs['tieredFee'+i][0].focus();
      flag = false;
      break ;
    }else if(!item.firstFee>0){
      self.$refs['firstFee'+i][0].focus();
      flag = false;
      break ;
    }else if((item.maxWeight <= 0&&i+1!=ruleSize)||(item.maxWeight!=-1&&item.maxWeight<=0&&i+1==ruleSize)){
      self.$refs['maxWeight'+i][0].focus();
      flag = false;
      break ;
    }else if(item.maxWeight<item.firstWeight&&i+1!=ruleSize){
      self.$refs['maxWeight'+i][0].focus();
      flag = false;
      break ;
    };
    //验证下一级配置首重必须是上一级最大重量
    if(i>0){
      let itemPre = tiyData.tParam.postage.postageRules[i-1];
      if(item.firstWeight!=itemPre.maxWeight){
        self.$refs['firstWeight'+i][0].focus();
        flag = false;
        break ;
      }
    }
  };
  if(!flag){
    self.$Message.error("运费配置错误!");
    tiyData.tStatus.modifyModal.isLoading = false;
    setTimeout(() => {
      self.$nextTick(() => {
        tiyData.tStatus.modifyModal.isLoading = true;
      });
    }, 200);
    return;
  }

  self.$refs['postageForm'].validate((valid) => {
    if (valid) {
      self.$Spin.show();
      self.$apiOther.savePostage(
        {postage:tiyData.tParam.postage,
          selectedCountryIds:tiyData.tParam.selectedCountryIds}
      ).then(function (response) {
        tiyData.tStatus.modifyModal.isLoading = false;

        if (response.success) {
          self.$Message.success("操作成功!");
          webList(self,tiyData);
        } else {
          self.$Message.error(response.errorMessage);
        }
        self.$Spin.hide();
        tiyData.tStatus.modifyModal.isShow=false;
      })

    }else{
      tiyData.tStatus.modifyModal.isLoading = false;
      setTimeout(() => {
        self.$nextTick(() => {
          tiyData.tStatus.modifyModal.isLoading = true;
        });
      }, 200);
    }
  })
}

export const actionDelete=(self,tiyData)=>{
  self.$Spin.show();
  self.$apiOther.deletePostage({
    "postageId":tiyData.tStatus.postageId,
    "postageName":tiyData.tStatus.postageName
  }).then(function (response) {
    self.$Spin.hide();
    if(response.success){
      self.$Message.success("操作成功!");
      webList(self,tiyData);
    }else{
      self.$Spin.hide();
      self.$Message.error(response.errorMessage);
    }

  })
}

export const getCountries=(self,tiyData)=>{
  // self.$Spin.show();
  self.$apiOther.getCountryList({}).then(function (response) {
    // self.$Spin.hide();
    tiyData.tData.countryList.splice(0,tiyData.tData.countryList.length);
    if(response.success){
      response.result.forEach(function(item){
        tiyData.tData.countryList.push(item);
      });
    }else{
      // self.$Spin.hide();
      self.$Message.error(response.errorMessage);
    }
  })
}

export const modify=(self,tiyData)=>{
  self.$refs['postageForm'].resetFields();
  self.$Spin.show();
  self.$apiOther.getPostage({
    "postageId":tiyData.tStatus.postageId,
    "withCounties": true
  }).then(function (response) {
    self.$Spin.hide();
    if(response.success) {
      tiyData.tParam.postage = response.result;
      if (tiyData.tParam.postage.countries) {
        tiyData.tParam.postage.countries.forEach(function (item) {
          tiyData.tParam.selectedCountryIds.push(item.countryId);
        });
      }
      tiyData.tStatus.modifyModal.isShow=true;
    }else{
      self.$Spin.hide();
      self.$Message.error(response.errorMessage);
    }
  })
}
